package com.apanal.qlife.sys.controller;

import java.util.List;

import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import com.apanal.qlife.common.annotation.CurrentUser;
import com.apanal.qlife.common.annotation.LogType;
import com.apanal.qlife.common.annotation.SystemLog;
import com.apanal.qlife.common.constants.Constants;
import com.apanal.qlife.common.page.Page;
import com.apanal.qlife.common.page.PageReturn;
import com.apanal.qlife.sys.model.Group;
import com.apanal.qlife.sys.model.User;
import com.apanal.qlife.sys.service.GroupService;
import com.apanal.qlife.sys.service.ResourceService;
import com.apanal.qlife.sys.service.RoleService;

/**
 * 群组控制类
 * 
 * 
 * @author shuliangxing
 * 
 * @date 2015-7-21下午5:20:44
 */
@Controller
@RequestMapping("/group")
public class GroupController {

	@Autowired
	private GroupService service;

	@Autowired
	private ResourceService resourceService;

	@Autowired
	private RoleService roleService;

	/**
	 * 分页查询
	 * 
	 * @param group
	 * @param page
	 * @param model
	 * @return
	 */
	@SystemLog(module = "群组管理", operation = "分页查询群组", type = LogType.SELECT)
	@RequiresPermissions("users:group:view")
	@RequestMapping(value = "list", method = RequestMethod.GET)
	public String list(Group group, Page page, Model model) {
		PageReturn<Group> result = service.selectRelationForPage(group, page);
		model.addAttribute(Constants.DEFAULT_RESULT_NAME, result);
		return "sys/groupList";
	}

	/**
	 * 转向搜索弹出框页面
	 * 
	 * @return
	 */
	@RequestMapping(value = "toListOpen", method = RequestMethod.GET)
	public String toListOpen() {
		return "sys/groupListOpen";
	}

	/**
	 * 分页查询,json返回
	 * 
	 * @param group
	 * @param page
	 * @param model
	 * @return
	 */
	@ResponseBody
	@RequestMapping(value = "listJson", method = RequestMethod.GET)
	public PageReturn<Group> listJson(Group group, Page page, Model model) {
		return service.selectRelationForPage(group, page);
	}

	/**
	 * 转向新增页面
	 * 
	 * @param model
	 * @return
	 */
	@RequiresPermissions("users:group:create")
	@RequestMapping(value = "/create", method = RequestMethod.GET)
	public String toCreate(Model model) {
		setCommonData(model);
		model.addAttribute("group", new Group());
		model.addAttribute("op", "新增群组");
		return "sys/groupEdit";
	}

	/**
	 * 新增
	 * 
	 * @param group
	 * @param redirectAttributes
	 * @return
	 */
	@SystemLog(module = "群组管理", operation = "新增群组", type = LogType.CREATE)
	@RequiresPermissions("users:group:create")
	@ResponseBody
	@RequestMapping(value = "/create", method = RequestMethod.POST)
	public String create(@CurrentUser User user, Group group) {
		group.setInsertBy(user.getUseId());
		return service.insert(group);
	}

	/**
	 * 转向更新页面
	 * 
	 * @param groId
	 * @param model
	 * @return
	 */
	@RequiresPermissions("users:group:update")
	@RequestMapping(value = "/{id}/update", method = RequestMethod.GET)
	public String toUpdate(@PathVariable("id") Long groId, Model model) {
		setCommonData(model);
		model.addAttribute("group", service.selectRelationById(groId));
		model.addAttribute("op", "修改群组");
		return "sys/groupEdit";
	}

	/**
	 * 更新
	 * 
	 * @param group
	 * @param redirectAttributes
	 * @return
	 */
	@SystemLog(module = "群组管理", operation = "修改群组", type = LogType.UPDATE)
	@RequiresPermissions("users:group:update")
	@ResponseBody
	@RequestMapping(value = "/{id}/update", method = RequestMethod.POST)
	public String update(@CurrentUser User user, Group group) {
		group.setLastUpdateBy(user.getUseId());
		return service.updateByIdSelective(group);
	}

	/**
	 * 转向详情页面
	 * 
	 * @param id
	 * @param model
	 * @return
	 */
	@RequiresPermissions("users:group:view")
	@RequestMapping(value = "/{id}/detail", method = RequestMethod.GET)
	public String toDetail(@PathVariable("id") Long id, Model model) {
		model.addAttribute("group", service.selectRelationById(id));
		model.addAttribute("op", "查看群组");
		return "sys/groupDetail";
	}

	/**
	 * 硬删除
	 * 
	 * @param user
	 * @param id
	 * @return
	 */
	@SystemLog(module = "群组管理", operation = "删除群组", type = LogType.DELETE)
	@RequiresPermissions("users:group:delete")
	@ResponseBody
	@RequestMapping(value = "/{id}/delete", method = RequestMethod.GET)
	public String delete(@PathVariable("id") Long id) {
		return service.deleteById(id);
	}

	/**
	 * 启用/停用
	 * 
	 * @param user
	 * @param group
	 * @return
	 */
	@SystemLog(module = "群组管理", operation = "启用/停用群组", type = LogType.UPDATE)
	@RequiresPermissions("users:group:start/stop")
	@ResponseBody
	@RequestMapping(value = "/{groId}/{normal}/updateToEnableOrDisable", method = RequestMethod.GET)
	public String updateToEnableOrDisable(@CurrentUser User user, Group group) {
		group.setLastUpdateBy(user.getUseId());
		return service.updateToEnableOrDisable(group);
	}

	/**
	 * 检查群组名是否存在,修改时排除当前群组名称
	 * 
	 * @param groId
	 *            群组id,新增时传0
	 * @param groName
	 *            群组名称
	 * @return
	 */
	@ResponseBody
	@RequestMapping(value = "/{groId}/{groName}/checkGroupNameExist", method = RequestMethod.GET)
	public Boolean checkGroupNameExist(@PathVariable("groId") Long groId,
			@PathVariable("groName") String groName) {
		return service.checkGroupNameExist(groId, groName);
	}

	/**
	 * 根据id字符串查询
	 * 
	 * @param ids
	 *            逗号分隔
	 * @return
	 */
	@ResponseBody
	@RequestMapping(value = "/{ids}/selectByIds", method = RequestMethod.GET)
	public List<Group> selectByIds(@PathVariable("ids") String ids) {
		return service.selectByIds(ids);
	}

	/**
	 * 设置公共数据
	 * 
	 * @param model
	 */
	private void setCommonData(Model model) {
		model.addAttribute("roleList", roleService.selectAll());
		model.addAttribute("resourceList", resourceService.selectResourceAuthTree());
	}

}
